package com.intelligentmeterreadingsys.report.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.intelligentmeterreadingsys.R;
import com.intelligentmeterreadingsys.util.SharedPreferenceUtil;

import dbhelper.XmlNameHelper;

/**
 * 报案界面
 * 
 * @author king
 * 
 */
public class ReportMainActivity extends Activity implements OnClickListener {
    public Button button1;// RFID表报案

    public Button button3;// 其它报案
    public Button button4;// 一键上传
    public Button button5;// 报案查询
    public boolean isSafe = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

	super.onCreate(savedInstanceState);
	setContentView(R.layout.report_main);
	init();
    }

    /**
     * 初始化控件
     */
    public void init() {
	button1 = (Button) findViewById(R.id.order_main_rfid);

	button3 = (Button) findViewById(R.id.order_main_other);
	button4 = (Button) findViewById(R.id.order_main_upload);
	button5 = (Button) findViewById(R.id.order_main_query);
	button1.setOnClickListener(this);

	button3.setOnClickListener(this);
	button4.setOnClickListener(this);
	button5.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
	Intent intent = null;
	switch (v.getId()) {
	case R.id.order_main_rfid:
	    intent = new Intent(ReportMainActivity.this, ReportAddActivity.class);
	    startActivity(intent);
	    break;

	case R.id.order_main_query:
	    intent = new Intent(ReportMainActivity.this, OrderQueryMainActivity.class);
	    startActivity(intent);
	    break;
	case R.id.order_main_other:
	    SharedPreferenceUtil sh = new SharedPreferenceUtil(ReportMainActivity.this);
	    boolean b = sh.getBoolean(XmlNameHelper.isSafe);
	    String pass = sh.getValue(XmlNameHelper.passWord);
	    if (!pass.equals("") || !b) {
		showDialog();

	    } else {
		System.out.println("----------------");
		intent = new Intent(this, OrderOtherActivity.class);
		startActivity(intent);

	    }
	    break;
	case R.id.order_main_upload:
	    intent = new Intent(this, ReportUploadActivity.class);
	    startActivity(intent);
	    break;
	default:
	    break;
	}

    }

    public boolean showDialog() {

	LayoutInflater factory = LayoutInflater.from(this);
	final View textEntryView = factory.inflate(R.layout.dialog, null);
	final EditText editTextPass = (EditText) textEntryView.findViewById(R.id.txt_password);

	AlertDialog.Builder ad1 = new AlertDialog.Builder(this);
	ad1.setIcon(android.R.drawable.ic_dialog_info);
	ad1.setView(textEntryView);
	ad1.setTitle("安全验证");
	ad1.setPositiveButton("确定", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		String pass = editTextPass.getEditableText().toString();
		SharedPreferenceUtil sh = new SharedPreferenceUtil(ReportMainActivity.this);
		if (pass.equals(sh.getValue(XmlNameHelper.passWord))) {
		    Intent intent = new Intent(ReportMainActivity.this, OrderOtherActivity.class);
		    startActivity(intent);
		} else {
		    Toast.makeText(ReportMainActivity.this, "密码错误", 1000).show();
		    isSafe = false;
		}

	    }
	});
	ad1.setNegativeButton("取消", new android.content.DialogInterface.OnClickListener() {

	    @Override
	    public void onClick(DialogInterface dialog, int which) {
		isSafe = false;

	    }
	});
	ad1.create();
	ad1.show();
	return isSafe;

    }

    public void SafeShow(Class<?> activityClass) {
	Intent intent = null;
	SharedPreferenceUtil sh = new SharedPreferenceUtil(ReportMainActivity.this);
	boolean b = sh.getBoolean(XmlNameHelper.isSafe);
	String pass = sh.getValue(XmlNameHelper.passWord);
	if (!pass.equals("") || !b) {
	    boolean bb = showDialog();

	    if (!bb) {
		return;
	    } else {
		System.out.println("----------------");
		intent = new Intent(this, activityClass);
		startActivity(intent);

	    }
	}

    }

}
